from apps.common.base.dao import BaseDao
from apps.merchant.coupon.serializers import *
from apps.common.extension.customException import *
from apps.pay.order.dao import OrderDao

class CouponDao(BaseDao):
    MODEL_CLASS = Coupon
    SERI_CLASS = CouponBaseSerializer

    def create(self, data):
        try:
            if data.get('types') == '2':
                orderId = data.get('code')
                if not orderId:
                    raise VerifyException('订单ID缺失')
                if orderId:
                    order = OrderDao().findOneByOrderId(orderId)
                    if not order:
                        raise VerifyException('订单不存在')
                    data['user'] = order.user.id
            return super().create(data)
        except Exception as e:
            raise e